<?php

class ProfileController extends Zend_Controller_Action
{
    public function init(){
        $userModel = new Application_Model_User();
        if (!$userModel->isLoggedIn()) {
            $this->_redirect('/');
        }

        $this->view->headLink()->appendStylesheet('http://blueimp.github.com/cdn/css/bootstrap.min.css');
        $this->view->headLink()->appendStylesheet('http://blueimp.github.com/cdn/css/bootstrap-responsive.min.css');
    }
    
    public function indexAction()
    {
        $userModel = new Application_Model_User();
        $userId = $userModel->getSession()->userId;
        $userInfo = $userModel->getDetailUser($userId);
        
        $directory = APPLICATION_PATH . '/../public/images/users/'. $userId;
        $listImage = $this->getDirectoryList($directory);
        //print_r($listImage);
        $this->view->listImage = $listImage;
        $this->view->userInfo = $userInfo;
    }
    
    public function uploadImageAction()
    {
        require('My\UploadImage\UploadHandler.php');
        $upload_handler = new UploadHandler();
        exit;
    }
    
    function getDirectoryList ($directory) 
    {
        // create an array to hold directory list
        $results = array();
        
        // create a handler for the directory
        @$handler = opendir($directory);
        
        // open directory and walk through the filenames
        while (@$file = readdir($handler)) {
        
          // if file isn't this directory or its parent, add it to the results
          if ($file != "." && $file != "..") {
            $results[] = $file;
          }
        
        }
        
        // tidy up: close the handler
        closedir($handler);
        
        // done!
        return $results;
    }
    
    public function editProfileAction()
    {
        
    }
}
